using System;
using Microsoft.VisualStudio.Package;
using Microsoft.Build.BuildEngine;

namespace MH.WsdlWorks.ProjectModel
{
    public class BuildProject
    {
        public BuildProject(BuildEngine parentEngine)
        {
            this.parentEngine = parentEngine;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectFileType">one of ProjectFileConstants</param>
        /// <returns></returns>
        public string GetEvaluatedProperty(string projectFileType)
        {
            return null;
        }

        private string fullFileName;
        public string FullFileName
        {
            get { return this.fullFileName; }
            set { this.fullFileName = value; }
        }

        BuildPropertyGroup evaluatedProperties = null;
        public BuildPropertyGroup EvaluatedProperties
        {
            get
            {
                this.RefreshProjectIfDirty();
                if (this.evaluatedProperties == null)
                {
                    this.evaluatedProperties = new BuildPropertyGroup();
                }
                return this.evaluatedProperties.Clone(false);
            }
        }

        private void RefreshProjectIfDirty()
        {
            
        }

        BuildEngine parentEngine;
        public BuildEngine ParentEngine
        {
            get { return parentEngine; }
            set { parentEngine = value; }
        }

        BuildItemGroup evaluatedItems = null;
        public BuildItemGroup EvaluatedItems
        {
            get
            {
                this.RefreshProjectIfDirty();
                if (this.evaluatedItems == null)
                {
                    this.evaluatedItems = new BuildItemGroup();
                }
                return this.evaluatedItems.Clone(false);
            }
        }

        TargetCollection targets = null;
        public TargetCollection Targets
        {
            get
            {
                System.Diagnostics.Debug.Assert(this.targets != null, "Project object not initialized.  targets is null.");
                return this.targets;
            }
        }

        public void SetProperty(string propertyName, string propertyValue)
        {

        }

        public void SetProperty(string propertyName, string propertyValue, string condition)
        {
            
        }

        public BuildItemGroup GetEvaluatedItemsByName(string itemName)
        {
            return new BuildItemGroup();
        }

        BuildPropertyGroup globalProperties;
        public BuildPropertyGroup GlobalProperties
        {
            get
            {
                return this.globalProperties;
            }
            set
            {
                this.globalProperties = value.Clone(true);
            }
        }

        public bool Build(string targetName)
        {
            return true;
        }

        public void Save(string projectFileName)
        {
            this.fullFileName = projectFileName;
            System.Diagnostics.Debug.Assert(this.wsdlProject != null, "wsdlProject is null");
            ProjectModelSerializer.Serialize(this.wsdlProject, projectFileName);
        }

        public BuildPropertyGroup AddNewPropertyGroup(bool insertAtEndOfProject)
        {
            return null;
        }

        public string GetProjectExtensions(string id)
        {
            return string.Empty;
        }

        public void SetProjectExtensions(string id, string content)
        {
        }

        bool isDirty = false;
        public bool IsDirty
        {
            get { return isDirty; }
        }

        public BuildItem AddNewItem(string itemName, string itemInclude)
        {
            return new BuildItem(itemName, itemInclude);
        }

        public void RemoveItem(BuildItem itemToRemove)
        {
        }

        public string[] GetConditionedPropertyValues(string propertyName)
        {
            return new string[] { };
        }

        BuildPropertyGroupCollection propertyGroups = null;
        public BuildPropertyGroupCollection PropertyGroups
        {
            get
            {
                return this.propertyGroups;
            }
        }

        public void RemovePropertyGroupsWithMatchingCondition(string matchCondition)
        {
            
        }

        internal void Load(string fullProjectPath)
        {
            this.fullFileName = fullProjectPath;
            this.wsdlProject = ProjectModelSerializer.Deserialize(fullProjectPath);
        }

        WsdlProject wsdlProject;
        public WsdlProject WsdlProject
        {
            get { return wsdlProject; }
            set { wsdlProject = value; }
        }
    }
}
